﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LimitOrderBookSim.Assets
{
    public class Limit : IComparable
    {
        public double Price { get; set; }
        public int Size { get; set; }
        public int Volume { get; set; }

        private List<int> _orders;

        public List<int> Orders
        {
            get
            {
                lock (_orders)
                {
                    return _orders;
                }
            }
            set
            {
                _orders = value;
            }

        }

        public object Sync = new object();

        public Limit()
        {
            Orders = new List<int>();
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int hash = 17;
                hash = hash * 23 + Price.GetHashCode();
                return hash;
            }
        }

        public override bool Equals(object obj)
        {
            bool result = false;

            if (null != obj && this.GetType() == obj.GetType())
            {
                if (((Limit)obj).Price == this.Price)
                {
                    result = true;
                }
            }
            return result;
        }

        public override string ToString()
        {
            String result = string.Empty;
            result += "LIMIT: " + Price + Environment.NewLine;
            result += "     Size: " + Size + Environment.NewLine;
            result += "     Volume: " + Volume + Environment.NewLine;
            result += "     Orders: " + Orders.Count + Environment.NewLine;

            //foreach (int order in Orders)
            //{
            //    result += "         " + order.ToString() + Environment.NewLine;
            //}

            return result;
        }

        public int CompareTo(object obj)
        {
            int result = 1;

            if (null != obj && this.GetType() == obj.GetType())
            {
                result = Price.CompareTo(((Limit)obj).Price);
            }

            return result;
        }
    }
}
